﻿
@{
    ViewBag.Title = "添加商家";
}
@section CSS{
    <link href="~/Content/css/layerui/layui.css" rel="stylesheet" />
}
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <section class="content-header">
        <h1>
            @ViewBag.text
            <small>商家</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="#">商家管理</a></li>
            <li><a href="/shop/company">商家列表</a></li>
            <li class="active">@ViewBag.text 商家</li>
        </ol>
    </section>
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box">
                    <div class="box-body">
                        <div class="ibox float-e-margins">

                            <div class="ibox float-e-margins">
                                <div class="ibox-content">
                                    <form class="form-horizontal" id="from-add" novalidate="novalidate" method="post" action="/shop/AddorEditCompanyInfo">

                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">公司名称 <span style="color:red;">*</span>：</label>
                                            <div class="col-sm-9">
                                                <input type="text" name="company_name" required="" aria-required="true" id="company_name" class="form-control" placeholder="公司名称，与营业执照上一致">
                                                <span class="help-block m-b-none"></span>
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">社会信用代码：</label>
                                            <div class="col-sm-9">
                                                <input type="text" name="organizing_bar" id="organizing_bar" value="" class="form-control" placeholder="营业执照上的社会信用代码">
                                                <span class="help-block m-b-none"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">公司地址：</label>
                                            <div class="col-sm-9">
                                                <select style="width: 120px; height: 30px;" id="selProvice" name="selProvice"><option value="">请选择</option><option value="1">北京</option><option value="2">天津</option><option value="3">河北省</option><option value="4">山西省</option><option value="5">内蒙古</option><option value="6">辽宁省</option><option value="7">吉林省</option><option value="8">黑龙江省</option><option value="9">上海</option><option value="10">江苏省</option><option value="11">浙江省</option><option value="12">安徽省</option><option value="13">福建省</option><option value="14">江西省</option><option value="15">山东省</option><option value="16">河南省</option><option value="17">湖北省</option><option value="18">湖南省</option><option value="19">广东省</option><option value="20">广西</option><option value="21">海南省</option><option value="22">重庆</option><option value="23">四川省</option><option value="24">贵州省</option><option value="25">云南省</option><option value="26">西藏自治区</option><option value="27">陕西省</option><option value="28">甘肃省</option><option value="29">青海省</option><option value="30">宁夏</option><option value="31">新疆</option><option value="32">台湾省</option><option value="33">香港</option><option value="34">澳门</option><option value="35">海外</option></select>
                                                <select style="width: 120px; height: 30px; display: none;" name="selCity" id="selCity"></select>
                                                <select style="width: 120px; height: 30px; display: none;" name="selCounty" id="selCounty"></select>
                                                <span class="help-block m-b-none"></span>
                                                <input type="text" name="address" id="address" required="" aria-required="true" class="form-control" placeholder="请输入地址">
                                                <span class="help-block m-b-none"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">营业执照有效期：</label>
                                            <div class="col-sm-9">
                                                <input type="text" name="expiry_date" id="expiry_date" placeholder="营业执照有效期" onclick="" class="form-control pull-right">
                                                <span class="help-block m-b-none"></span>
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">上传图片：</label>
                                            <div class="col-sm-3">
                                                <div class="layui-upload">
                                                    <button type="button" class="layui-btn" id="test1">营业执照</button><input class="layui-upload-file" type="file" accept="undefined" name="file">
                                                    <div class="layui-upload-list">
                                                        <img class="layui-upload-img" id="business_llicence_img" onclick="javascript:imgzoom(this.id)" style="width: 150px; height: 150px; cursor: pointer">
                                                        <p id="demoText1"></p>
                                                    </div>
                                                </div>
                                                <span class="help-block m-b-none">图片大小不超过1M</span>

                                            </div>
                                            <div class="col-sm-3">
                                                <div class="layui-upload">
                                                    <button type="button" class="layui-btn" id="test2">法人身份证正面</button><input class="layui-upload-file" type="file" accept="undefined" name="file">
                                                    <div class="layui-upload-list">
                                                        <img class="layui-upload-img" id="face_card_img" onclick="javascript:imgzoom(this.id)" style="width: 150px; height: 150px; cursor: pointer">
                                                        <p id="demoText2"></p>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="col-sm-3">
                                                <div class="layui-upload">
                                                    <button type="button" class="layui-btn" id="test3">法人身份证反面</button><input class="layui-upload-file" type="file" accept="undefined" name="file">
                                                    <div class="layui-upload-list">
                                                        <img class="layui-upload-img" id="opposite_card_img" onclick="javascript:imgzoom(this.id)" style="width: 150px; height: 150px; cursor: pointer">
                                                        <p id="demoText3"></p>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">法人姓名：</label>
                                            <div class="col-sm-9">
                                                <input type="text" name="corporation" id="corporation" value="" class="form-control" placeholder="法人">
                                                <span class="help-block m-b-none"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">法人电话：</label>
                                            <div class="col-sm-9">
                                                <input type="text" name="corporation_phone" id="corporation_phone" value="" class="form-control" placeholder="法人电话">
                                                <span class="help-block m-b-none"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">法人身份有效期：</label>
                                            <div class="col-sm-9">

                                                <input type="text" name="card_date" id="card_date" placeholder="法人身份证有效期" class="form-control pull-right">
                                                <span class="help-block m-b-none"></span>
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">联系人姓名：</label>
                                            <div class="col-sm-9">
                                                <input type="text" name="real_name" id="real_name" value="" class="form-control" placeholder="入驻联系人姓名">
                                                <span class="help-block m-b-none"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">联系人电话：</label>
                                            <div class="col-sm-9">
                                                <input type="text" name="contacts_phone" id="contacts_phone" value="" class="form-control" placeholder="入驻联系人电话">
                                                <span class="help-block m-b-none"></span>
                                            </div>
                                        </div>
                                        <div class="hr-line-dashed"></div>

                                        <input type="hidden" id="id" value="0" name="id">
                                        <input type="hidden" id="opposite_card" value="" name="opposite_card">
                                        <input type="hidden" id="business_llicence" value="" name="business_llicence">
                                        <input type="hidden" id="face_card" value="" name="face_card">
                                        <input type="hidden" id="province" value="0" name="province">
                                        <input type="hidden" id="city" value="0" name="city">
                                        <input type="hidden" id="country" value="0" name="country">
                                        <div class="form-group">
                                            <div class="col-sm-4 col-sm-offset-3">
                                                <button class="btn btn-primary" type="submit">保存信息</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

</div>

@section JS{
    <script src="~/Content/js/jquery.form.js"></script>
    <script src="~/Content/plugins/validate/jquery.validate.min.js"></script>
    <script src="~/Content/plugins/validate/messages_zh.min.js"></script>

    <script type="text/javascript">
        var _id = "@ViewBag.id";
        $('#expiry_date').datepicker({
            autoclose: true
        });
        $('#card_date').datepicker({
            autoclose: true
        });
        $(function () {
            getselctedText();
            if (_id != undefined && _id!="0") getinfo(_id);
            else {
                getprovince(0, "#selProvice", "");//供应商地址
            }
        });


        function getinfo(_id) {
            ajax("/shop/CompanyInfo/" + _id, {}, function (data) {
                if (data!= null) {
                    $("#id").val(data.id);
                    $("#company_name").val(data.company_name);
                    $("#organizing_bar").val(data.organizing_bar);
                    $("#expiry_date").val(data.expiry_date);
                    $("#address").val(data.address);
                    $("#corporation").val(data.corporation);
                    $("#corporation_phone").val(data.corporation_phone);
                    if (data.business_llicence!=null&&data.business_llicence!="") {
                        $("#business_llicence_img").attr('src', domain + data.business_llicence);
                    }
                    if (data.face_card != null && data.face_card != "") {
                        $("#face_card_img").attr('src', domain + data.face_card);
                    }
                    if (data.opposite_card != null && data.opposite_card != "") {
                        $("#opposite_card_img").attr('src', domain + data.opposite_card);
                    }
                    $("#business_llicence").val(data.business_llicence);
                    $("#face_card").val(data.face_card);
                    $("#opposite_card").val(data.opposite_card);
                    //获取省市城市列表
                    getprovince(0, "#selProvice", data.province);
                    getprovince(data.province, "#selCity", data.city);
                    getprovince(data.city, "#selCounty", data.country);
                    $("#province").val(data.province);
                    $("#city").val(data.city);
                    $("#country").val(data.country);

                    $("#card_date").val(data.card_date);
                    $("#real_name").val(data.real_name);
                    $("#contacts_phone").val(data.contacts_phone);
                    //公司唯一，禁止修改
                    $("#company_name").attr("readonly", "readonly");
                }
                else {
                    layer.alert('信息获取失败，请重试！', { icon: 5 });
                }
            }, true, "", "get");
        }

        $("#from-add").validate({
            ajaxPost: true,
            submitHandler: function (form) {
                $(form).ajaxSubmit({
                    dataType: "json",
                    success: function (result) {
                        if (result.status == "y") {
                            layer.msg("操作成功", { icon: 6, time: 1200 }, function () {
                                window.location.href = "/shop/company";
                            });
                        }
                        else {
                            layer.msg(result.info, { icon: 5, time: 2000 });
                        }
                    }
                });
            }
        });

        //图片点击跳转
        function imgzoom(obj) {
            var img = document.getElementById(obj).src
            if (!!img) {
                onclick = window.open(img)
            }
        }
        //后期可重构一个方法
        layui.use('upload', function () {
            var $ = layui.jquery
                , upload = layui.upload;
            //普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                , url: domain + "/tools/UploadPCImage?type=company"
                ,size:1024
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#business_llicence_img').attr('src', result); //图片链接（base64）

                    });
                },

                done: function (res) {
                    $('#business_llicence_img').attr('src', domain + res.info);
                    $('#business_llicence').val(sqlImgdomain+res.info);
                }
                , error: function () {
                    //，并实现重传
                    var demoText = $('#demoText1');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });
            var uploadInst = upload.render({
                elem: '#test2'
                , url: domain + "/tools/UploadPCImage?type=company"
                 , size: 1024
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#face_card_img').attr('src', result); //图片链接（base64）
                    });
                }
                , done: function (res) {
                    $('#face_card_img').attr('src', domain + res.info);
                    $('#face_card').val(sqlImgdomain+res.info);
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText2');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });
            var uploadInst = upload.render({
                elem: '#test3',
                url: domain + "/tools/UploadPCImage?type=company",
                size: 1024,
                before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#opposite_card_img').attr('src', result); //图片链接（base64）
                    });
                },
                done: function (res) {
                    $('#opposite_card_img').attr('src', domain + res.info);
                    $('#opposite_card').val(sqlImgdomain+res.info);
                },
                error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText3');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });
        });

        function getselctedText() {
            //省县城
            $("#address").val($("#selProvice").find("option:selected").text());
            $("#selCounty").change(function () {
                $("#address").val($("#selProvice").find("option:selected").text() + "," + $("#selCity").find("option:selected").text() + "," + $("#selCounty").find("option:selected").text());
            });

        }

        function getprovince(id, obj, sid) {
            ajax("/tools/GetPrivoceList/" + id, {}, function (data) {
                var ui = "<option value=''>请选择</option>";
                $.each(data, function (row, value) {
                    ui += "<option value='" + value.id + "'>" + value.name + "</option>";
                })
                $(obj).html(ui);
                $(obj).show();
                if (sid != undefined && sid != '')
                    $(obj).val(sid);
            })
        }


        $("#selProvice").change(function () {
            var _mId = $(this).val();
            if (_mId != "") {
                getprovince(_mId, "#selCity");
                $("#province").val(_mId);
            }
            else { $("#selCity").html("<option value=''>请选择城市</option>"); }
            $("#selCity").hide();
        });
        $("#selCity").change(function () {
            var _mId = $(this).val();
            if (_mId != "") {
                getprovince(_mId, "#selCounty");
                $("#city").val(_mId);
            }
            else { $("#selCounty").html("<option value=''>请选择区域</option>"); }
        });
        $("#selCounty").change(function () {
            var _mId = $(this).val();
            if (_mId != "") {
                $("#country").val(_mId);
            }
        });

    </script>
}

